#include <FBFlow.h>
#include <Opencv.h>
#include <matching.h>
#include <compareFeatures.h>
#include <SPRH2d.h>
#include <plot.h>
#include <combineImage.h>
#include <stateManipulator.h>

#define MODE 1 //0:save mode 1:comparemode



int main (int argc, char **argv)
{
  cv::Mat src_img;

  // string dirname("0918/dgc/33");
  //string dirname("0123/dgc/left/white/10");
  string dirname("project-dsc/0128/origin_2");
  compareCloth cc;
  compareCloth cs;
  cc.init("data", 5, 11);
  cs.init("step", 1, 5);

  cc.setState(4);
  cs.setState(1);

  int image_count = 1;
  int end_count = 211;
  int save_count = 1;

  firstProc(dirname, image_count, src_img);
  resizeSrcImg(src_img,src_img, 0.8, INTER_LINEAR);
  FB fb;
  fb.firstProc(src_img);

  vector<Point2f> flowpoint, flowscale;
  vector<Point2f> boxedflowpoint, boxedflowscale;
  vector< pair<P2d,P2d> > vals;

  while (1)
    {
      fb.Proc(dirname, image_count, src_img, flowpoint, flowscale);

      float max = 15.0;
      float min = 2.0;
      vector<Point2f> largeflowpoint, largeflowscale;

      extractRangeFlow(flowpoint, flowscale, &largeflowpoint, &largeflowscale,  max, min);

      for( uint i = 0 ; i < flowscale.size() ; i++)
      	{
      	  flowscale[i].x *=3;
      	  flowscale[i].y *=3;
       	}

      for( uint i = 0 ; i < largeflowscale.size() ; i++)
      	{
      	  largeflowscale[i].x *=3;
      	  largeflowscale[i].y *=3;
      	}
      convertflowPoint2P2d(largeflowpoint, largeflowscale, &vals);
      vector<Point2f> largeflowpoint2, largeflowscale2;
      extractRangeFlow(flowpoint, flowscale, &largeflowpoint2, &largeflowscale2, 8, 0.0001);

      cv::Mat flow_image;
      display(16, src_img, flow_image, vals, "flow_image");

      char c = waitKey(10);

      if(c == '\x1b')
	{
	  break;
	}
      if(image_count >1 && (image_count %2) == 0)
      	{
	  cout << "image_count " << image_count << endl;
	  cc.genVecFiles(largeflowpoint2, largeflowscale2, src_img);
	  cc.genAllFlowFile(largeflowpoint2, largeflowscale2);
      	}

      if(c == 'w')
	{
	  waitKey(0);
	}

      if(image_count == end_count)
	break;

      image_count += 1;
    }
  return 0;
}
